from PyQt4 import QtCore, QtGui

colors = [QtCore.Qt.red, QtCore.Qt.black, QtCore.Qt.yellow, QtCore.Qt.blue, QtCore.Qt.darkRed]


class Brick(QtGui.QGraphicsItem):
    def __init__(self, brickId=0):
        super(Brick, self).__init__()
        self.brickId = brickId
        self.xPos = 0
        self.yPos = 0
        self.widthSize = 0
        self.heightSize = 0
        self.xForward = 2
        self.yForward = 2
        self.dx = 1
        self.dy = 1
        self.rect = QtCore.QRectF(self.xPos, self.yPos, self.widthSize, self.heightSize)
        self.setPos(self.xPos, self.yPos)
        self.brickType = 1
        self.invincible = 0

    def setForward(self, xForward, yForward):
        self.xForward = xForward
        self.yForward = yForward

    def getRect(self):
        return (self.xPos, self.yPos, self.widthSize, self.heightSize)

    def setRect(self, x, y, width, height):
        self.xPos = x
        self.yPos = y
        self.widthSize = width
        self.heightSize = height
        self.rect = QtCore.QRectF(self.xPos, self.yPos, self.widthSize, self.heightSize)

    def boundingRect(self):
        return self.rect

    def paint(self, painter, *args):
        global colors
        color = colors[self.brickId]
        brush = QtGui.QBrush()
        brush.setStyle(QtCore.Qt.SolidPattern)
        brush.setColor(color)
        painter.setBrush(brush)
        painter.drawRect(self.rect)

    dx = 20
    dy = 20

    def nextPos(self):
        if self.brickType == 0:
            return
        self.xPos += self.dx * self.xForward
        self.yPos += self.dy * self.yForward
        self.setPos(self.xPos, self.yPos)


class UserBrick(Brick):
    def __init__(self, brickId):
        super(UserBrick, self).__init__(brickId)

    def keyNextPos(self, keyEvent):
        if keyEvent.key() == QtCore.Qt.Key_Left:
            self.xPos = self.xPos - self.dx
        elif keyEvent.key() == QtCore.Qt.Key_Right:
            self.xPos = self.xPos + self.dx
        elif event.key() == QtCore.Qt.Key_Up:
            self.yPos = self.yPos - self.dy
        elif event.key() == QtCore.Qt.Key_Down:
            self.yPos = self.yPos + self.dy
        self.setPos(self.xPos, self.yPos)
